package cn.nubia.admin.application.service;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.application.bean.Feedback;
import cn.nubia.admin.application.query.FeedbackQuery;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by justin on 15/7/12.
 */
@Service
public class FeedbackService extends BaseServiceImpl<Feedback>{
    @Autowired
    SystemLogService systemLogService;

    public Paged<Feedback> listFeedbacks(FeedbackQuery feedbackQuery) {
        Query query = Query.build(Feedback.class);
        if(feedbackQuery.getStatus()!=null && feedbackQuery.getStatus() >=0){
            query.addEq(Feedback.COLUMN_STATE, feedbackQuery.getStatus());
        }else{
            query.addNotEq(Feedback.COLUMN_STATE, Feedback.STATE_DEL);
        }

        if(feedbackQuery.getMarketVersion()!=null && !"".equals(feedbackQuery.getMarketVersion())){
            query.addLike(Feedback.COLUMN_MARKETVERSION, feedbackQuery.getMarketVersion());
        }
        if(feedbackQuery.getCreateTimes()!=null && feedbackQuery.getCreateTimee() != null){
            query.addBetween(Feedback.COLUMN_CREATETIME, DateUtil.getIntegerTime(feedbackQuery.getCreateTimes()), DateUtil.getIntegerTime(feedbackQuery.getCreateTimee()));
        }
        query.addOrder(Feedback.COLUMN_CREATETIME, Query.DBOrder.DESC);
        query.setPaged(feedbackQuery.getPageNo(), feedbackQuery.getPageSize());
        Paged<Feedback> feedbackPaged = findPagedByQuery(query);
        for(Feedback feedback : feedbackPaged.getListData()){
            feedback.setCreateTimeShow(DateUtil.getFormatDate(feedback.getCreateTime()));
            feedback.setStatusName(Feedback.statusMap.get(String.valueOf(feedback.getStatus())));
        }
        return feedbackPaged;
    }

    public Feedback getFeedback(Integer id) {
        Feedback feedback = get(id);
        feedback.setCreateTimeShow(DateUtil.getFormatDate(feedback.getCreateTime()));
        feedback.setReplyTimeShow(DateUtil.getFormatDate(feedback.getReplyTime()));
        return feedback;
    }

    public void processAndSave(Feedback feedback) {
        feedback.setReplyTime(DateUtil.getCurrentTime());
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        feedback.setAdminId(adminId);
        feedback.setStatus(Feedback.STATE_REPLAYED);
        update(feedback);
        //log
        systemLogService.log(SystemLog.ModuleTypeEnum.FEEDBACKLIST, SystemLog.OperateTypeEnum.REPLY,feedback.getId());
    }
}
